package chainsMod;

public class code {

    // 定义抽象过滤器类
    abstract class Filter {

        protected Filter next;

        // 返回传入的参数，支持链式调用
        public Filter setNext(Filter next) {
            this.next = next;
            return this; // 返回当前对象，支持链式调用
        }

        public abstract boolean resolve(Trouble trouble);
    }

    // 字符编码转换过滤器
    class CharacterEncodingFilter extends Filter {

        @Override
        public boolean resolve(Trouble trouble) {
            if ("CharacterEncoding".equals(trouble.getKind())) {
                System.out.println("CharacterEncodingFilter is processing: " + trouble.getKind());
                trouble.setKind("Processed: " + trouble.getKind());
                return true;
            }
            if (next != null) {
                return next.resolve(trouble);
            }
            return false;
        }
    }

    // 数据类型转换过滤器
    class DataTypeConversionFilter extends Filter {

        @Override
        public boolean resolve(Trouble trouble) {
            if ("DataTypeConversion".equals(trouble.getKind())) {
                System.out.println("DataTypeConversionFilter is processing: " + trouble.getKind());
                trouble.setKind("Processed: " + trouble.getKind());
                return true;
            }
            if (next != null) {
                return next.resolve(trouble);
            }
            return false;
        }
    }

    // 数据校验过滤器
    class DataValidationFilter extends Filter {

        @Override
        public boolean resolve(Trouble trouble) {
            if ("DataValidation".equals(trouble.getKind())) {
                System.out.println("DataValidationFilter is processing: " + trouble.getKind());
                boolean isValid = true; // 假设校验通过
                if (isValid) {
                    trouble.setKind("Validated: " + trouble.getKind());
                    return true;
                }
            }
            if (next != null) {
                return next.resolve(trouble);
            }
            return false;
        }
    }

    // 定义问题类，用于过滤器处理
    class Trouble {

        private String kind;

        public Trouble(String kind) {
            this.kind = kind;
        }

        public String getKind() {
            return kind;
        }

        public void setKind(String kind) {
            this.kind = kind;
        }
    }

}
